package com.itnan.user.service;

import com.itnan.user.dao.CustomerDao;
import com.itnan.user.dao.UserDao;
import com.itnan.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tangjian on 27/4/14.
 */
@Component
@Transactional(readOnly = false)
public class UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private CustomerDao customerDao;

    public void save(User user) {
        userDao.save(user);
    }

    public User findByUserNameAndUserPassword(String userName, String userPassword) {
        return userDao.findByUserNameAndUserPassword(userName, userPassword);
    }

    public User findByUserName(String userName) {
        return userDao.findByUserName(userName);
    }

    ;

    public ArrayList<User> findAll() {
        ArrayList<User> list = new ArrayList<User>();
        for (User user : userDao.findAll()) {
            user.setTotal(customerDao.findByUserId(user.getUserId()).size());
            list.add(user);
        }
        return list;
    }

    public int getCustomerSize() {
        int sum = 0;
        for (User user : userDao.findAll()) {
            int total = customerDao.findByUserId(user.getUserId()).size();
            sum += total;
        }
        return sum;
    }

}
